#! /bin/sh

source /etc/rc.d/common

MDADM="/sbin/mdadm"
DFLT_OPTS="--assemble --scan"

check_args ${@}

SCRIPT="$(/usr/bin/basename ${0})"
if check_opts_set ${SCRIPT}; then
    USER_OPTS="$(get_rc_opts ${SCRIPT})"
    if [ ${?} -ne 0 ]; then
        /bin/echo ${USER_OPTS}
        exit 1
    fi
else
    USER_OPTS="${DFLT_OPTS}"
fi

start() {
    /bin/echo "Assembling software RAID devices..."
    eval ${MDADM} ${USER_OPTS} || exit 1
}

stop() {
    /bin/echo "Stopping MD RAID arrays..."
    ${MDADM} --detail --scan | while read -r row; do
        md_path="$(echo ${row} | awk '{ print $2 }')"
        md_name="$(echo ${row} | awk '{ print $4 }' | \
            cut -d= -f2 | cut -d: -f2)"
        # Only stop arrays that aren't the ESOS boot media
        if ! echo ${md_name} | egrep 'esos_boot|esos_root|esos_conf|esos_logs' \
            > /dev/null; then
            ${MDADM} --stop ${md_path} || exit 1
        fi
    done
}

status() {
    # We don't know anything
    exit ${UNKNOWN}
}

# Perform specified action
${1}
